<?php
/*
PWision extension: Newspaper http://pw-newspaper.googlecode.com/
Copyright (C) 2010, 2012 Becheru Petru-Ioan

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
include_once("inc/PFormSection.inc");
include_once('inc/PVirtualKeyboard.inc');
include_once('inc/PRedirectSection.inc');
include_once("pw-newspaper.inc");
/** 	The KeywordSearch class is designed to model the section that allows a user to search for articles containg certain keywords.
	*	\latexonly \label{PWision:Extensions:Newspaper:KeywordSearch} \index{KeywordSearch} \index{search} \index{TValue} \endlatexonly
	*	\brief search for articles using keywords.
	*	\version 1.6.12
	*/
class KeywordSearch extends PBasicFormSection
	{
	function getTitle()
		{
		return parent::getTitle().( $this->wasParamSet('kw') ?( ': '. $this->getParam('kw') ):'');
		}
	protected function preprocessParam($param_name)
		{
		$_REQUEST[$param_name]=charset_decode_utf_8($_REQUEST[$param_name]);
		$_REQUEST[$param_name]=str_replace('&#160;',' ',$_REQUEST[$param_name]);// nbsp as a special char not allowed. fix for a chromium bug
		}
	function setParams()
		{
		$this
			->addParam('kw','/^[-&#;:\/,a-z0-9. ]{0,256}$/i','')
			->addParam('page','/^[0-9]{0,2}$/i','')
			;
		}
	function Head()
		{
		return
			parent::Head()
				->Insert(
					Object(new PSimpleTag("meta"))
						->setAtr("name","robots")
						->setAtr("content",'noindex')
					)
				->Insert( PVirtualKeyboard::Head(Main()->getLanguage(),array('kw')) )
				;
		}
	/// constructor
	function KeywordSearch()
		{
		PBasicFormSection::PBasicFormSection();
		$this->TheForm
				->setAtr('method','post')
			->Insert( PTextlang('Keyword:') )
			->Insert(
				Object(
					new PInput(
						"text",
						$this->getParam('kw'),
						"kw"
						)
					)
					->setAtr('id','kw')
					->setAtr('autocomplete','off')
				)
			->Insert( new PInput("submit",lang("Keyword search!"),"") )
			->Insert(
				PVirtualKeyboard::kbSelect()
				)
			;
		}
	function	Chapter2Title()
		{
		if( !$this->wasParamSet('kw') || $this->getParam('kw')=='' ) return null;
		return PTextlang('Search results:');
		}
	function	Chapter2()
		{
		$ret=new PContent;
		$displayed_results=20;
		$results_count=0;
		$limit=($this->wasParamSet('page')?$this->getParam('page'):0)*$displayed_results;

		$ul=new PStructure(new PElement("li"), new PElement("ul"));
		if( !$this->wasParamSet('kw') || $this->getParam('kw')=='' )
				{//keyword cloud
				$ul=new PStructure(
					new PElement("li"),
					Object(new PElement("ul"))
						->setAtr('class','KwSCloud')
					);
				$displayed_results=100;
				$limit=($this->wasParamSet('page')?$this->getParam('page'):0)*$displayed_results;
				$Keywords=Main()->DB->fetch_all_query("select TValue `Keyword`, COUNT(*) `Freq` from NArticleTag where IDTag=4 group by TValue ORDER BY `Freq` DESC limit $limit, $displayed_results");
				$results_count=count($Keywords);
				foreach($Keywords as $Keyword_row)
					{
					$Keyword=$Keyword_row['Keyword'];
					$Freq=$Keyword_row['Freq'];
					$ul->newKid()
								->setAtr('class','KwSCloud')
							->Insert(
								Sections::Link2(
									'KeywordSearch',
									lang("Search for")." [ $Keyword ]",
									"$Keyword<sub>($Freq)</sub>",
									Object(new PURLAttributes)
										->setAtr('kw',$Keyword)
									)
									->setAtr('class','kw')
								)
							->Insert(new PText(', ') )
							;
					}
				}
			else
				{
				if ( $textSearchForm=Main()->SearchForm( $this->getParam('kw') ) )
					$ret
						->Insert(PTextlang('You can try a text search:'))
						->Insert( $textSearchForm );
				$articles=array();
					$articles=Main()->DB->fetch_all_query(
					//return new PText(
		"select
			x.`ID`, L2.TValue `Title`
		from
			(
SELECT DISTINCT L1.IDArticle `ID` 
FROM NArticleTag `L1`, NArticle
WHERE (
L1.IDTag =4
OR L1.IDTag =3
OR L1.IDTag =1
OR L1.IDTag =11
)
AND L1.TValue =  '".$this->getParam('kw')."' AND NArticle.ID=L1.IDArticle order by Volume DESC, Issue DESC, firstPage DESC, lastPage DESC, `ID` ASC
		limit $limit, $displayed_results
		) `x`,
			NArticleTag `L2`
where
	x.`ID`=L2.IDArticle and L2.IDTag='1'
" );
				$results_count=count($articles);
				if( $results_count==0 )// No articles found
					return $ret->InsertFirst(
										El('p')
											->Insert(PTextlang('No articles found!'))
										);
				foreach($articles as $article)
					$ul->newKid()->Insert(
						Sections::Link2(
							'Article',$article['Title'],$article['Title'],
							Object(new PURLAttributes)
								->setAtr('ID',$article['ID'])
							)
						);
				}
		if($this->getParam('page')>0)
			$ul->newFirstKid()
						->setAtr('class','KwSPrevNext')
					->Insert(
						Sections::Link2(
							'KeywordSearch','&laquo; &laquo; &laquo; '.$limit.' '.lang('previous results').' &laquo; &laquo; &laquo;','&laquo; &laquo; &laquo; '.$limit.' '.lang('previous results').' &laquo; &laquo; &laquo;',
							Object(new PURLAttributes)
								->setAtr('kw', $this->getParam('kw') )
								->setAtr('page', $this->getParam('page')-1 )
							)
						)
				;
		if($results_count==$displayed_results )
			{
			$ul->newKid()
						->setAtr('class','KwSPrevNext')
					->Insert(
						Sections::Link2(
							'KeywordSearch','&raquo; &raquo; &raquo; '.lang('next results').' &raquo; &raquo; &raquo;','&raquo; &raquo; &raquo; '.lang('next results').' &raquo; &raquo; &raquo;',
							Object(new PURLAttributes)
								->setAtr('kw', $this->getParam('kw') )
								->setAtr('page', $this->getParam('page')+1 )
							)
						)
				;
			}
		return $ret->InsertFirst($ul);
		}
	}
?>